<?php

function __autoload($class_name)
{
    if (file_exists(DS . 'classes/' . $class_name . '.class.php'))
    {
        require_once (DS . 'classes/' . $class_name . '.class.php');
    }
}

function isConnected()
{
    $index = Index::getInstance();

    //On va chercher les infos de l'user
    $arrayInfos = $index->getInfosClient();

    //On check si il y a une session valide avec le SID retourné
    $index->global_connhist->getConnHistoBySID($arrayInfos["sidget"]);

    //echo "Error:".$index->global_connhist->errormsg;
    if ($index->global_connhist->requeteOk)
    {
        //Session trouvé, on vérifie maintenant la sécurité de la session
        $verifSession = $index->global_connhist->verifSession($arrayInfos["hostget"], $arrayInfos["clientget"]);

        //Si non valide on force l'user à se relogger, sinon on utilise la session existante
        if (!$verifSession)
        {
            $err = 3; //Vos paramètres de session ne correspondent pas
            //On supprime la session
            $index->global_connhist->closeSession();

            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        return false;
    }
}

function isEtudiant()
{
    if (isConnected ())
    {
        global $global_member;
        if ($global_member instanceof Etudiant)
        {
            return true;
        }
    }
    return false;
}

function isAdministrateur()
{
    if (isConnected ())
    {
        global $global_member;
        //var_dump($global_member);
        $types = $global_member->getIdTypeUser();
        $type = explode(",", $types);
        //var_dump($types);
        if (in_array("1", $type))
        {
            return true;
        }
    }
    return false;
}

function isEnseignant()
{
    if (isConnected ())
    {
        global $global_member;
        if ($global_member instanceof Enseignant)
        {
            return true;
        }
    }
    return false;
}

function isResponsable()
{
    if (isConnected ())
    {
        global $global_member;
        $types = explode(",", $global_member->getIdTypeUser());
        if (in_array("2", $types))
        {
            return true;
        }
    }
    return false;
}

function isEntreprise()
{
    if (isConnected ())
    {
        global $global_member;
        if ($global_member instanceof ContactEntreprise)
        {
            return true;
        }
    }
    return false;
}

function isTuteurEntreprise()
{
    if (isConnected ())
    {
        global $global_member;
        $types = explode(",", $global_member->getIdTypeUser());
        if (in_array("4", $types))
        {
            return true;
        }
    }
    return false;
}
function isTuteurEnseignant()
{
    if (isConnected ())
    {
        global $global_member;
        $types = explode(",", $global_member->getIdTypeUser());
        if (in_array("3", $types))
        {
            return true;
        }
    }
    return false;
}

function redirectTo($url="")
{
    ?>
    <script type="text/javascript">
        <?php if(!empty($url)){
            echo 'window.location.href="'.$url.'";';
        }else{
            echo 'window.location.href="'.URL.'";';
        }?>
    </script>
    <?php
}

function enableDocument()
{
	return true;
}
/* Fonction de debug */

function vardump($var)
{
    echo "<pre>";
    var_dump($var);
    echo "</pre>";
}

function alert($text="")
{
    echo '<script text="text/javascript">alert("';
    if (!empty($text))
    {
        echo $text;
    }
    else
    {
        echo "ok?";
    }
    echo '");</script>';
}

?>